import { NgModule, ModuleWithProviders, APP_INITIALIZER, Optional, SkipSelf } from '@angular/core';
import { Initializer } from './utils/common.utils';
import { I18NService } from './services';
import { HttpInterceptorProviders } from './core';

@NgModule({
  imports: [],
  declarations: [],
  exports: []
})
export class SharedModule {
  constructor(@Optional() @SkipSelf() parentModule: SharedModule) {
    if (parentModule) {
      // shareModule 只能注入一次
      throw new Error('SharedModule is already loaded. Import it in the AppModule only');
    }
  }

  static forRoot(): ModuleWithProviders<SharedModule> {
    return {
      ngModule: SharedModule,
      providers: [
        HttpInterceptorProviders,
        {
          provide: APP_INITIALIZER,
          useFactory: Initializer.load,
          deps: [I18NService],
          multi: true
        }
      ]
    };
  }
}
